package top.kaoshanji.r1ch04a;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.testng.annotations.Test;

import java.io.IOException;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/4 19:09
 */
public class BeanFactoryTest {

    /**
     * 代码清单test-r1ch04a-93页b：从BeanFactory中获取Bean对象
     * @throws IOException
     */
    @Test
    public void getBean() throws IOException {

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource res = resolver.getResource("classpath:r1ch04a/beanfactory-car-beans.xml");
        System.out.println(res.getURL());

        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); // 把读取到的 Bean 定义信息 存储到 BeanFactory
        reader.loadBeanDefinitions(res);

        System.out.println("init BeanFactory...");

        Car car = factory.getBean("car", Car.class); // 此时触发了容器初始化动作
        System.out.println("此时有了一个可用的 car 对象..");

        car.introduce(); // 使用从容器中获取到的对象..

    }



}
